<?php
/**
 * Created by PhpStorm.
 * User: czw
 * Date: 2018/7/11
 * Time: 14:59
 */
namespace app\common\model;

use app\admin\validate\IDMustBePositiveInt;
use app\lib\exception\BaseException;
use app\lib\exception\DeleteException;
use app\lib\exception\NotFoundException;
use app\lib\exception\UpdateException;
use think\Db;

class Admin extends BaseModel
{
    public function role(){
        return $this->belongsToMany('Role');
    }
    /**
     * 新增管理员
     * @param $data
     * @return false|int
     */
    public static function createOne($data)
    {
        $model = new self();
        $admin = $model->where(['name' => $data['name']])->find();
        if ($admin) throw new BaseException(['msg' => '管理员已存在！', 'errorCode' => 90001]);
        $res = $model->allowField(true)->save($data);
        if(!$res) throw (new BaseException());//创建失败，经过验证的数据不能正确保存，只能报系统异常
        return $res;
    }

    /**
     * 查找所有管理员
     * @return false|\PDOStatement|string|\think\Collection
     */
    public static function getAll()
    {
        return self::select();
    }

    /**
     * 根据管理员id查找管理员
     * @param $id
     * @return null|static
     * @throws NotFoundException
     */
    public static function getById($id)
    {
        $res = self::with('role.permission')->find($id);
        if(!$res) throw (new NotFoundException());
        return $res;
    }

    /**
     * 更新管理员
     * @param $id
     * @param $data
     * @return $this
     * @throws UpdateException
     */
    public static function updateOne($id, $data)
    {
        (new IDMustBePositiveInt())->check($id);
        $res = self::where('id=' . $id)->update($data);
        if (!$res) {
            throw (new UpdateException());//修改失败抛出异常
        }
    }
    /**
     * 更新管理员自身
     * @param $admin_id
     * @param $data
     * @return $this
     * @throws UpdateException
     */
    public static function updateSelf($data)
    {
        $id = 0;//应该token中获取用户信息
        $res = self::where('id=' . $id)->update($data);
        if (!$res) {
            throw (new UpdateException());//修改失败抛出异常
        }
    }

    /**
     *
     * @param $id
     * @return int
     * @throws NotFoundException
     */
    public static function deleteOne($id)
    {
        $admin = self::find($id);
        if (!$admin) throw (new NotFoundException());
        $res = self::destroy($id);
        if(!$res) throw (new DeleteException());
    }
    public function getPermissions($id){
        //先获取role_ids
        $admin_role = Db::table(config('database.prefix').'admin_role')->where('admin_id='.$id)->field('role_id')->select();
        $role_ids = '';
        foreach ($admin_role as $v){
            $role_ids .= $v['role_id'].',';
        }
        //再获取auth_ids
        $permissions = Db::table(config('database.prefix').'Permission')->where('id', 'in', trim($role_ids,','))->select();
        return $permissions;
    }
    public function getRoles($id){

    }
}